package cn.wolfcode.luowowo.listener;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.redis.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.service.IStrategyService;
import javafx.scene.input.ContextMenuEvent;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class RedisDateInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    //readis 初始化数据
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEventEvent) {
        //查询所有的攻略
        List<Strategy> list = strategyService.list();
        //遍历集合，把所有属性封装成VO对象。
        for (Strategy strategy : list) {
            if(strategyStatisVORedisService.isVoExists(strategy.getId())){
                continue;
            }
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(strategy.getId());
            strategyStatisVORedisService.setStrategyStatisVo(vo);
        }
    }
}
